#include "nr_object_manager.h"

nr_object_manager* nr_object_manager::g_object_manager = NULL;

nr_object_manager::nr_object_manager(QObject *parent)
    : nr_manager_base(parent)
    , m_device_type_manager(m_io_manager)
    , m_device_item_manager(m_device_type_manager, m_io_manager)
    , m_channel_manager(m_device_item_manager)
    , m_scheduler_manager(m_device_item_manager, m_channel_manager, m_io_manager)
{
}

nr_object_manager::~nr_object_manager()
{
}

nr_object_manager& nr_object_manager::instance()
{
    if (g_object_manager == NULL)
    {
        g_object_manager = new nr_object_manager;
        Q_ASSERT(g_object_manager);
    }

    return *g_object_manager;
}

void nr_object_manager::destroy()
{
    delete g_object_manager;
    g_object_manager = NULL;
}

nr_error_t nr_object_manager::load()
{
    nr_error_t status;
    
    // Load the io first
    status = m_io_manager.load();
    if (NR_FAILED(status))
        return status;
    
    status = m_device_type_manager.load();
    Q_ASSERT(status == NR_ERROR_NONE);

    /*  
        status = m_device_type_manager.save_as_xml("DeviceSchema_2.xml");
        Q_ASSERT(status == NR_ERROR_NONE);
    */

    status = m_device_item_manager.load();
    Q_ASSERT(status == NR_ERROR_NONE);

    /* 
        status = m_device_item_manager.save_as_xml("DeviceItems2.xml");
        Q_ASSERT(status == NR_ERROR_NONE);
    */

    /*   
        status = m_user_role_manager.load();
        Q_ASSERT(status == NR_ERROR_NONE);
    */

    status = m_scheduler_manager.load();
    Q_ASSERT(status == NR_ERROR_NONE);
    
    return status;
}

nr_error_t nr_object_manager::unload()
{
    m_device_type_manager.unload();
    m_device_item_manager.unload();
    m_scheduler_manager.unload();
    m_user_role_manager.unload();
    
    return NR_ERROR_NONE;
}
